public class test2 {
    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode() {}
     *     TreeNode(int val) { this.val = val; }
     *     TreeNode(int val, TreeNode left, TreeNode right) {
     *         this.val = val;
     *         this.left = left;
     *         this.right = right;
     *     }
     * }
     */
    class Solution {
        public void flatten(TreeNode root) {
            //递归
            //1、左子树变成链表
            //2、右子树变成链表
            //3、右子树放在左子树最右边(分两步：左子树移到右子树上、右子树转到左子树最右)
            if(root==null){
                return;
            }
            //1
            flatten(root.left);
            //2
            flatten(root.right);
            //先把右子树记录下来
            TreeNode tmp = root.right;
            //左子树放到右子树上
            root.right = root.left;
            //左子树置空
            root.left = null;
            //3
            //找到树的最右节点
            while(root.right!=null){
                root = root.right;
            }
            root.right = tmp;
        }
    }
}
